package com.tanhua.server.controller;

import com.tanhua.commons.utils.JwtUtils;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private UserInfoService userInfoService;
    /**
     * 查询用户资料
     *      1.请求头：token
     *      2.请求用户参数：userID
     */
    @GetMapping
    public ResponseEntity users(@RequestHeader("Authorization") String token,Long userID){
        if (userID==null){
            userID=Long.valueOf(UserHolder.getId());
        }
        UserInfoVo userInfo = userInfoService.findById(userID);
        return ResponseEntity.ok(userInfo);
    }
    /**
     * 更新用户资料
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestHeader("Authorization") String token,@RequestBody UserInfo userInfo){
        userInfo.setId(UserHolder.getId());
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }
}
